package server;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class MessageDecoder extends CumulativeProtocolDecoder {
	private CharsetDecoder	decoder;

	public MessageDecoder() {
		Charset charset = Charset.forName("UTF-8");
		decoder = charset.newDecoder();
	}

	protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
		String msg = in.getString(decoder);
		out.write(new Message(msg));
		return true;
	}
}
